var username
var password
function login() {
    username = $("#username");
    password = $("#password");
    //console.log(username.val());
    // $(".loginSuccess").val("欢迎您，bearBaby");
    if(!username.val() || username.val() === " "){
        showMsg("请输入用户名");
        username.focus();
        return false;
    }
    if(!password.val() || password.val() === " "){
        showMsg("请输入密码");
        password.focus ();
        return false;
    }

    $.ajax({
        type: "post",
        url : "http://iot.wduozhi.xyz/api/user/login",
        data: $('#loginForm').serialize(),  //获取表单数据
        dataType: "json",
        success: function (data) {
            console.log(data);
            if(data.status === "success"){
                localStorage.setItem('username',username.val())
                localStorage.setItem('nickname',data.data.nick_name)
                localStorage.setItem('email',data.data.email)
                localStorage.setItem('tel',data.data.tel)
                $("#myModal").click();      //关闭登录窗口
                window.location.href = "mainWindows.html"

            }else{
                showMsg("账号或密码错误");
            }
        },
        error: function () {
            console.log("error!");
            showMsg("500：登陆失败")
        }
    })
}

function showMsg(msg) {
    $(".alert").text( msg);
}

//监听回车键提交
$(function(){
    document.onkeydown=keyDownSearch;
    function keyDownSearch(e) {
        // 兼容FF和IE和Opera
        var theEvent = e || window.event;
        var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
        if (code === 13) {
            $('#submit').click();//具体处理函数
            return false;
        }
        return true;
    }
});

function isLogin() {
    $.ajax({
        type: "get",
        url: "http://iot.wduozhi.xyz/api/gateway",
        dataType: "json",
        success: function (data) {
            if(data.status === 'error'){
                // alert("请先登录！！")
                // window.location.href = "../index.html"
            }
        },
        error: function () {
            console.log("error!");
        }
    })
}


